package com.acquisition.config.security.handler;

import com.acquisition.common.entity.BaseResponse;
import com.acquisition.common.enums.ResultCode;
import com.alibaba.fastjson.JSON;
import org.springframework.security.web.session.SessionInformationExpiredEvent;
import org.springframework.security.web.session.SessionInformationExpiredStrategy;
import org.springframework.stereotype.Component;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @Author Jay Chou
 * @Description 会话信息过期策略
 * @Date 2023-04-12 10:58
 **/
@Component
public class CustomizeSessionInformationExpiredStrategy implements SessionInformationExpiredStrategy {
    @Override
    public void onExpiredSessionDetected(SessionInformationExpiredEvent sessionInformationExpiredEvent) throws IOException {
        HttpServletResponse httpServletResponse = sessionInformationExpiredEvent.getResponse();
        httpServletResponse.setContentType("text/json;charset=utf-8");
        httpServletResponse.getWriter().write(JSON.toJSONString(new BaseResponse<>(ResultCode.USER_ACCOUNT_USE_BY_OTHERS)));
    }
}
